﻿using IntelLocalVoiceAI.ResponseData;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoiceAI;
using VoiceAI.VoiceResponse;

namespace IntelLocalVoiceAI
{
    public static class ResponseCreator
    {
        private static readonly IReadOnlyDictionary<string, string> responseTextMap = new Dictionary<string, string>() 
        {
            {Skills.BrightnessMinMum,ResponseText.SetBrightnessOk },
            {Skills.BrightnessMaxMum,ResponseText.SetBrightnessOk },
            {Skills.BrightnessDown,ResponseText.SetBrightnessOk },
            {Skills.BrightnessUp,ResponseText.SetBrightnessOk },
            {Skills.BrightnessTo,ResponseText.SetBrightnessOk },
            {Skills.VolumeMinMum,ResponseText.SetVolumeOk },
            {Skills.VolumeMaxMum,ResponseText.SetVolumeOk },
            {Skills.SetVolumeDown,ResponseText.SetVolumeOk },
            {Skills.SetVolumeUp,ResponseText.SetVolumeOk },
            {Skills.VolumeTo,ResponseText.SetVolumeOk },
            {Skills.Lock,ResponseText.Ok },
        };
        private static readonly IReadOnlyDictionary<string, PCControlType> responseControlTypeMap = new Dictionary<string, PCControlType>()
        {
            {Skills.BrightnessMinMum,PCControlType.SetBrightnessTo },
            {Skills.BrightnessMaxMum,PCControlType.SetBrightnessTo },
            {Skills.BrightnessDown,PCControlType.AddBrightness },
            {Skills.BrightnessUp,PCControlType.AddBrightness },
            {Skills.BrightnessTo,PCControlType.SetBrightnessTo },
            {Skills.VolumeMinMum,PCControlType.SetVolumeTo },
            {Skills.VolumeMaxMum,PCControlType.SetVolumeTo},
            {Skills.SetVolumeDown,PCControlType.AddVolume},
            {Skills.SetVolumeUp,PCControlType.AddVolume},
            {Skills.VolumeTo,PCControlType.SetVolumeTo },
            {Skills.Lock,PCControlType.Lockscreen},
        };
        private static readonly IReadOnlyDictionary<string, string> responseAudioMap = new Dictionary<string, string>()
        {
            {Skills.BrightnessMinMum,Audios.SetBrightnessOk },
            {Skills.BrightnessMaxMum,Audios.SetBrightnessOk },
            {Skills.BrightnessDown,Audios.SetBrightnessOk },
            {Skills.BrightnessUp,Audios.SetBrightnessOk },
            {Skills.BrightnessTo,Audios.SetBrightnessOk },
            {Skills.VolumeMinMum,Audios.SetVolumeOk },
            {Skills.VolumeMaxMum,Audios.SetVolumeOk },
            {Skills.SetVolumeDown,Audios.SetVolumeOk },
            {Skills.SetVolumeUp,Audios.SetVolumeOk },
            {Skills.VolumeTo,Audios.SetVolumeOk },
            {Skills.Lock,Audios.Ok },
        };
        private static readonly IReadOnlyDictionary<string, double> defaultValueMap = new Dictionary<string, double>()
        {
            {Skills.BrightnessMinMum,0 },
            {Skills.BrightnessMaxMum,1 },
            {Skills.BrightnessDown,2 },
            {Skills.BrightnessUp,2 },
            {Skills.VolumeMinMum,0 },
            {Skills.VolumeMaxMum,1 },
            {Skills.SetVolumeDown,2 },
            {Skills.SetVolumeUp,2 },
            {Skills.Lock ,0},
        };
        private static readonly IReadOnlyDictionary<string, double> factorMap = new Dictionary<string, double>()
        {
            {Skills.BrightnessMinMum,1 },
            {Skills.BrightnessMaxMum,1 },
            {Skills.BrightnessDown,-1 },
            {Skills.BrightnessUp,1 },
            {Skills.BrightnessTo,1 },
            {Skills.VolumeMinMum,1 },
            {Skills.VolumeMaxMum,1 },
            {Skills.SetVolumeDown,-1 },
            {Skills.SetVolumeUp,1 },
            {Skills.VolumeTo,1 },
            {Skills.Lock ,1},
        };
        public static IResponseData GetResponseData(string data)
        {
            //把 音量 调 到 二十	SOUND.SET_VOLUME	20
            //把 音量 调 低 些	SOUND.VOLUME_DOWN
            //锁 屏	LOCK.LOCK
            if(string.IsNullOrEmpty(data))
            {
                Debug.WriteLine("GetResponseData prarm data con't be null.");
                return null;
            }
            
            string request = "";
            string response = "";
            string command = "";
            string value = "";
            string[] originalData = data.Replace("\n","").Split("\t");
            switch (originalData.Length)
            {
                case 2:
                    request = originalData[0].Replace(" ","");
                    command = originalData[1];
                    
                    break;
                case 3:
                    request = originalData[0].Replace(" ", "");
                    command = originalData[1];
                    value = originalData[2];
                    break;
                default:
                    Debug.WriteLine("GetResponseData prarm data format error");
                    break;
            }
            if (!responseControlTypeMap.ContainsKey(command))
            {
                Debug.WriteLine($"GetResponseData Skill {command} not support.");
                return null;
            }
            response = responseTextMap[command];
            var controlType = responseControlTypeMap[command];
            var url = responseAudioMap[command];
            PCControlResponseData pCControlResponseData = new PCControlResponseData();
            pCControlResponseData.ErrorCode = ErrorCode.NoError;
            pCControlResponseData.RequestText = request;
            pCControlResponseData.ResponseText = response;
            pCControlResponseData.Type = controlType;
            pCControlResponseData.TTSUrl = url;
            if(double.TryParse(value,out double v))
            {
                pCControlResponseData.Value = (v / 100) * factorMap[command];
            }
            else
            {
                if(!defaultValueMap.ContainsKey(command))
                {
                    Debug.WriteLine($"GetResponseData Skill {command} don't have default value.");
                    return null;
                }
                pCControlResponseData.Value = defaultValueMap[command] * factorMap[command];
            }
            return pCControlResponseData;
        }
        public static IResponseData GetErrorResponse()
        {
            DefaultResponseData defaultResponse = new DefaultResponseData
            {
                ErrorCode = ErrorCode.UnknowError,
                RequestText = "",
                ResponseText = "",
                RestoreInHistory = false,
                TTSUrl = ""
            };
            return defaultResponse;
        }
        public static IResponseData GetDefaultResponse(string defaultRequest,string defaultResponse,string defaultUrl)
        {
            DefaultResponseData defaultResponseData = new DefaultResponseData()
            {
                ErrorCode = ErrorCode.NoError,
                RequestText = defaultRequest,
                ResponseText = defaultResponse,
                TTSUrl = defaultUrl,
                RestoreInHistory = true,
            };
            return defaultResponseData;
        }
    }
}
